let cap = new Captcha(".cap", {
  refresh: false,
});
//验证用户名
jQuery.validator.addMethod(
  "userTest",
  function (value) {
    let reg = /^[\u4e00-\u9fa5a-z]+$/i;
    return reg.test(value);
  },
  "用户名由汉字或者字母组成"
);
//验证密码
jQuery.validator.addMethod(
  "passTest",
  function (value) {
    let reg =
      /^(?![0-9]+$)(?![a-z]+$)(?![A-Z]+$)(?![,\.;:\?]+$)[0-9a-zA-Z,,\.;:\?]{4,14}$/;
    return reg.test(value);
  },
  "密码格式错误"
);
//验证验证码
jQuery.validator.addMethod(
  "yanzheng",
  function () {
    return cap.validate($("#test").val());
  },
  "验证码输入错误"
);
$("#form").validate({
  rules: {
    registUser: {
      required: true,
      userTest: true,
    },
    registPass: {
      required: true,
      passTest: true,
    },
    test: {
      required: true,
      yanzheng: true,
    },
  },

  messages: {
    registUser: {
      required: "用户名是必填的",
    },
    registPass: {
      required: "密码是必填的",
    },
    test: {
      required: "验证码是必填的",
    },
  },
  submitHandler: function (form) {
    let p = pAjax({
      url: "/sa/regist.php",
      type: "get",
      data: {
        registUser: registUser.value,
        registPass: registPass.value,
      },
    });
    p.then((res) => {
      res = JSON.parse(res);
      if (res.code) {
        // setCookie("login", registUser.value);
        // let url = localStorage.getItem("url");
        // localStorage.removeItem("url");
        location.href = "../html/login.html";
      } else {
        alert(res.msg);
      }
    });
  },
});
// 回到顶部
var dingbu = document.querySelector(".dingbu");
window.onscroll = function () {
  var height = window.innerHeight + scrollY;
  if (height > window.innerHeight) {
    dingbu.style.display = "block";
    dingbu.onclick = function () {
      scrollTo({ top: 0, behavior: "smooth" });
    };
  } else {
    dingbu.style.display = "none";
  }
};
